package ejemplo_gui;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Principal.java
 *
 * Created on 12/01/2012, 09:27:15 AM
 */

/**
 *
 * @author ADM
 */
public class Principal extends javax.swing.JFrame {
    private double resultado;

    private char op=' ';

    /** Creates new form Principal */
    public Principal() {
        initComponents();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lbl_titulo = new javax.swing.JLabel();
        txt_probar = new javax.swing.JTextField();
        cmd_probar4 = new javax.swing.JButton();
        cmd_probar1 = new javax.swing.JButton();
        cmd_probar2 = new javax.swing.JButton();
        cmd_probar3 = new javax.swing.JButton();
        cmd_probar5 = new javax.swing.JButton();
        cmd_probar6 = new javax.swing.JButton();
        cmd_probar7 = new javax.swing.JButton();
        cmd_probar8 = new javax.swing.JButton();
        cmd_probar9 = new javax.swing.JButton();
        cmd_probarn = new javax.swing.JButton();
        cmd_probar0 = new javax.swing.JButton();
        cmd_probarp = new javax.swing.JButton();
        cmd_sumar = new javax.swing.JButton();
        cmd_borrar = new javax.swing.JButton();
        cmd_resultado = new javax.swing.JButton();
        cmd_restar = new javax.swing.JButton();
        cmd_multiplicar = new javax.swing.JButton();
        cmd_dividir = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        lbl_titulo.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        lbl_titulo.setText("Ejemplo de aplicacion Swing");

        cmd_probar4.setText(" 4");
        cmd_probar4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmd_probar4ActionPerformed(evt);
            }
        });

        cmd_probar1.setText("1");
        cmd_probar1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmd_probar1ActionPerformed(evt);
            }
        });

        cmd_probar2.setText(" 2");
        cmd_probar2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmd_probar2ActionPerformed(evt);
            }
        });

        cmd_probar3.setText("3");
        cmd_probar3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmd_probar3ActionPerformed(evt);
            }
        });

        cmd_probar5.setText("5");
        cmd_probar5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmd_probar5ActionPerformed(evt);
            }
        });

        cmd_probar6.setText("6");
        cmd_probar6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmd_probar6ActionPerformed(evt);
            }
        });

        cmd_probar7.setText("7");
        cmd_probar7.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmd_probar7ActionPerformed(evt);
            }
        });

        cmd_probar8.setText("8");
        cmd_probar8.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmd_probar8ActionPerformed(evt);
            }
        });

        cmd_probar9.setText("9");
        cmd_probar9.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmd_probar9ActionPerformed(evt);
            }
        });

        cmd_probarn.setText("(-)");
        cmd_probarn.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmd_probarnActionPerformed(evt);
            }
        });

        cmd_probar0.setText("0");
        cmd_probar0.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmd_probar0ActionPerformed(evt);
            }
        });

        cmd_probarp.setText(".");
        cmd_probarp.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmd_probarpActionPerformed(evt);
            }
        });

        cmd_sumar.setText("+");
        cmd_sumar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmd_sumarActionPerformed(evt);
            }
        });

        cmd_borrar.setText("Borrar");
        cmd_borrar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmd_borrarActionPerformed(evt);
            }
        });

        cmd_resultado.setText("=");
        cmd_resultado.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmd_resultadoActionPerformed(evt);
            }
        });

        cmd_restar.setText("-");
        cmd_restar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmd_restarActionPerformed(evt);
            }
        });

        cmd_multiplicar.setText("*");
        cmd_multiplicar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmd_multiplicarActionPerformed(evt);
            }
        });

        cmd_dividir.setText("/");
        cmd_dividir.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cmd_dividirActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txt_probar, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbl_titulo, javax.swing.GroupLayout.PREFERRED_SIZE, 205, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(cmd_probar4)
                            .addComponent(cmd_probar1)
                            .addComponent(cmd_probar7)
                            .addComponent(cmd_probarn))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cmd_probar2)
                                    .addComponent(cmd_probar8)
                                    .addComponent(cmd_probar5))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(cmd_probar3)
                                        .addGap(6, 6, 6)
                                        .addComponent(cmd_sumar))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(cmd_probar9)
                                            .addComponent(cmd_probar6))
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(cmd_multiplicar)
                                            .addComponent(cmd_restar))))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(cmd_resultado)
                                    .addComponent(cmd_borrar)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(cmd_probar0)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cmd_probarp)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                .addComponent(cmd_dividir)))))
                .addContainerGap(103, Short.MAX_VALUE))
        );

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cmd_probar1, cmd_probar4, cmd_probar7, cmd_probarn});

        layout.linkSize(javax.swing.SwingConstants.HORIZONTAL, new java.awt.Component[] {cmd_borrar, cmd_resultado});

        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(32, 32, 32)
                .addComponent(lbl_titulo)
                .addGap(32, 32, 32)
                .addComponent(txt_probar, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cmd_probar1)
                        .addComponent(cmd_probar2))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(cmd_probar3)
                        .addComponent(cmd_sumar)
                        .addComponent(cmd_borrar)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cmd_probar4)
                            .addComponent(cmd_probar5)
                            .addComponent(cmd_probar6))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cmd_probar7)
                            .addComponent(cmd_probar8)
                            .addComponent(cmd_probar9)
                            .addComponent(cmd_multiplicar))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(cmd_probarn)
                            .addComponent(cmd_probar0)
                            .addComponent(cmd_probarp)
                            .addComponent(cmd_dividir)))
                    .addComponent(cmd_resultado)
                    .addComponent(cmd_restar))
                .addContainerGap(85, Short.MAX_VALUE))
        );

        pack();
        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        java.awt.Dimension dialogSize = getSize();
        setLocation((screenSize.width-dialogSize.width)/2,(screenSize.height-dialogSize.height)/2);
    }// </editor-fold>//GEN-END:initComponents

    private void cmd_probar4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmd_probar4ActionPerformed
        // TODO add your handling code here:
        String cad=txt_probar.getText();
        txt_probar.setText(cad+"4");
    }//GEN-LAST:event_cmd_probar4ActionPerformed

    private void cmd_probar1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmd_probar1ActionPerformed
        // TODO add your handling code here:
        String cad=txt_probar.getText();
        txt_probar.setText(cad+"1");

    }//GEN-LAST:event_cmd_probar1ActionPerformed

    private void cmd_probar2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmd_probar2ActionPerformed
        // TODO add your handling code here:
        String cad=txt_probar.getText();
        txt_probar.setText(cad+"2");
    }//GEN-LAST:event_cmd_probar2ActionPerformed

    private void cmd_probar3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmd_probar3ActionPerformed
        // TODO add your handling code here:
        String cad=txt_probar.getText();
        txt_probar.setText(cad+"3");
    }//GEN-LAST:event_cmd_probar3ActionPerformed

    private void cmd_probar5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmd_probar5ActionPerformed
        // TODO add your handling code here:
         String cad=txt_probar.getText();
        txt_probar.setText(cad+"5");
    }//GEN-LAST:event_cmd_probar5ActionPerformed

    private void cmd_probar6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmd_probar6ActionPerformed
        // TODO add your handling code here:
         String cad=txt_probar.getText();
        txt_probar.setText(cad+"6");
    }//GEN-LAST:event_cmd_probar6ActionPerformed

    private void cmd_probar7ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmd_probar7ActionPerformed
        // TODO add your handling code here:
         String cad=txt_probar.getText();
        txt_probar.setText(cad+"7");
    }//GEN-LAST:event_cmd_probar7ActionPerformed

    private void cmd_probar8ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmd_probar8ActionPerformed
        // TODO add your handling code here:
         String cad=txt_probar.getText();
        txt_probar.setText(cad+"8");
    }//GEN-LAST:event_cmd_probar8ActionPerformed

    private void cmd_probar9ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmd_probar9ActionPerformed
        // TODO add your handling code here:
         String cad=txt_probar.getText();
         txt_probar.setText(cad+"9");
    }//GEN-LAST:event_cmd_probar9ActionPerformed

    private void cmd_probarnActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmd_probarnActionPerformed
        // TODO add your handling code here:
        String cad=txt_probar.getText();
        if (!cad.contains("-"))
           txt_probar.setText("-"+cad);
    }//GEN-LAST:event_cmd_probarnActionPerformed

    private void cmd_probar0ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmd_probar0ActionPerformed
        // TODO add your handling code here:
        String cad=txt_probar.getText();
        txt_probar.setText(cad+"0");
    }//GEN-LAST:event_cmd_probar0ActionPerformed

    private void cmd_probarpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmd_probarpActionPerformed
        // TODO add your handling code here:
        String cad=txt_probar.getText();
        if (!cad.contains("."))
         txt_probar.setText(cad+".");
    }//GEN-LAST:event_cmd_probarpActionPerformed

    private void cmd_sumarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmd_sumarActionPerformed
        // TODO add your handling code here:
        String cad=txt_probar.getText();
        resultado=resultado+Double.parseDouble(cad);
        txt_probar.setText("");
        op='+';

    }//GEN-LAST:event_cmd_sumarActionPerformed

    private void cmd_borrarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmd_borrarActionPerformed
        // TODO add your handling code here:
        resultado=0;
        txt_probar.setText("");
    }//GEN-LAST:event_cmd_borrarActionPerformed

    private void cmd_resultadoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmd_resultadoActionPerformed
        // TODO add your handling code here:
        switch (op)
        {
            case '+': resultado=resultado + Double.parseDouble(txt_probar.getText());
                      txt_probar.setText(Double.toString(resultado));
                      resultado=0;
                      op='=';
                      break;

            case '-': resultado=resultado - Double.parseDouble(txt_probar.getText());
                      txt_probar.setText(Double.toString(resultado));
                      resultado=0;
                      op='=';
                      break;

            case '*': resultado=resultado * Double.parseDouble(txt_probar.getText());
                      txt_probar.setText(Double.toString(resultado));
                      resultado=0;
                      op='=';
                      break;
            
            case '/': resultado=resultado / Double.parseDouble(txt_probar.getText());
                      txt_probar.setText(Double.toString(resultado));
                      resultado=0;
                      op='=';
                      break;
        }
        
    }//GEN-LAST:event_cmd_resultadoActionPerformed

    private void cmd_restarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmd_restarActionPerformed
        // TODO add your handling code here:
        String cad=txt_probar.getText();
        resultado=resultado+Double.parseDouble(cad);
        txt_probar.setText("");
        op='-';
    }//GEN-LAST:event_cmd_restarActionPerformed

    private void cmd_multiplicarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmd_multiplicarActionPerformed
        // TODO add your handling code here:
        String cad=txt_probar.getText();
        resultado=resultado+Double.parseDouble(cad);
        txt_probar.setText("");
        op='*';
    }//GEN-LAST:event_cmd_multiplicarActionPerformed

    private void cmd_dividirActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cmd_dividirActionPerformed
        // TODO add your handling code here:
        String cad=txt_probar.getText();
        resultado=resultado+Double.parseDouble(cad);
        txt_probar.setText("");
        op='/';
    }//GEN-LAST:event_cmd_dividirActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Principal().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cmd_borrar;
    private javax.swing.JButton cmd_dividir;
    private javax.swing.JButton cmd_multiplicar;
    private javax.swing.JButton cmd_probar0;
    private javax.swing.JButton cmd_probar1;
    private javax.swing.JButton cmd_probar2;
    private javax.swing.JButton cmd_probar3;
    private javax.swing.JButton cmd_probar4;
    private javax.swing.JButton cmd_probar5;
    private javax.swing.JButton cmd_probar6;
    private javax.swing.JButton cmd_probar7;
    private javax.swing.JButton cmd_probar8;
    private javax.swing.JButton cmd_probar9;
    private javax.swing.JButton cmd_probarn;
    private javax.swing.JButton cmd_probarp;
    private javax.swing.JButton cmd_restar;
    private javax.swing.JButton cmd_resultado;
    private javax.swing.JButton cmd_sumar;
    private javax.swing.JLabel lbl_titulo;
    private javax.swing.JTextField txt_probar;
    // End of variables declaration//GEN-END:variables

}
